package com.easemob.chatuidemo.activity;

import com.easemob.chatuidemo.Constant;
import com.easemob.chatuidemo.R;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class DiscoverFragment extends Fragment implements OnClickListener {
    
    private Context mContext;
    /**
     * 扫一扫
     */
    private RelativeLayout rv_discover_scan;
    /**
     * 附近的人
     */
    private RelativeLayout rv_discover_nearby_all;
    /**
     * 腹肌就能的司机
     */
    private RelativeLayout rv_discover_nearby_driver;
    /**
     * 附近的物流
     */
    private RelativeLayout rv_discover_nearby_wuliu;
    /**
     * 附近的师傅
     */
    private RelativeLayout rv_discover_nearby_master;
    /**
     * 附近的发货商
     */
    private RelativeLayout rv_discover_nearby_supplier;

    /**
     * 飞蚂蚁平台
     */
    private RelativeLayout rv_discover_flyint;

    private static final int NEARBY_ALL = 0;
    private static final int NEARBY_DRIVER = 1;
    private static final int NEARBY_WULIU = 2;
    private static final int NEARBY_MASTER = 3;
    private static final int NEARBY_SUPPLIER = 4;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_discover, container, false);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (savedInstanceState != null&& savedInstanceState.getBoolean("isConflict", false))
            return;
        
        mContext = getActivity();
        
        rv_discover_scan = (RelativeLayout) getActivity().findViewById(R.id.rv_discover_scan);
        rv_discover_nearby_all = (RelativeLayout) getActivity().findViewById(R.id.rv_discover_nearby_all);
        rv_discover_nearby_driver = (RelativeLayout) getActivity().findViewById(R.id.rv_discover_nearby_driver);
        rv_discover_nearby_wuliu = (RelativeLayout) getActivity().findViewById(R.id.rv_discover_nearby_wuliu);
        rv_discover_nearby_master = (RelativeLayout) getActivity().findViewById(R.id.rv_discover_nearby_master);
        rv_discover_nearby_supplier = (RelativeLayout) getActivity().findViewById(R.id.rv_discover_nearby_supplier);
        rv_discover_flyint = (RelativeLayout) getActivity().findViewById(R.id.rv_discover_flyint);
        
        rv_discover_scan.setOnClickListener(this);
        rv_discover_nearby_all.setOnClickListener(this);
        rv_discover_nearby_driver.setOnClickListener(this);
        rv_discover_nearby_wuliu.setOnClickListener(this);
        rv_discover_nearby_master.setOnClickListener(this);
        rv_discover_nearby_supplier.setOnClickListener(this);
        rv_discover_flyint.setOnClickListener(this);
        
        ImageView titleImage = (ImageView) getView().findViewById(R.id.iv_new_contact);
        titleImage.setOnTouchListener(new OnTouchListener() {
            
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO 标题栏按钮点击事件
                Toast.makeText(getActivity(), "点击了标题栏按钮", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (((MainActivity) getActivity()).isConflict) {
            outState.putBoolean("isConflict", true);
        } else if (((MainActivity) getActivity()).getCurrentAccountRemoved()) {
            outState.putBoolean(Constant.ACCOUNT_REMOVED, true);
        }
    }
    
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        intent.setClass(getActivity(), NearbyActivity.class);
        switch (view.getId()) {
            case R.id.rv_discover_scan:
                startActivity(new Intent(getActivity(), CaptureActivity.class));
                break;
            case R.id.rv_discover_nearby_all:
                //TODO 附近的人
                bundle.putInt("nearby", NEARBY_ALL);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.rv_discover_nearby_driver:
                //TODO 司机
                bundle.putInt("nearby", NEARBY_DRIVER);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.rv_discover_nearby_master:
                //TODO 师傅
                bundle.putInt("nearby", NEARBY_MASTER);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.rv_discover_nearby_wuliu:
                //TODO 物流
                bundle.putInt("nearby", NEARBY_WULIU);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.rv_discover_nearby_supplier:
                //TODO 发货商
                bundle.putInt("nearby", NEARBY_SUPPLIER);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.rv_discover_flyint:
                //飞蚂蚁平台
                startActivity(new Intent(getActivity(), WebViewAvtivity.class));
                break;

            default:
                break;
        }
    }

}
